package kr.hkit.password.fragment.item;

import java.util.ArrayList;
import java.util.Calendar;

import kr.hkit.password.R;
import kr.hkit.password.dao.InfoDao;
import kr.hkit.password.db.MyDatabaseManager;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.ContentValues;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class ItemUpdate extends Fragment implements OnClickListener {
	private static final String TAG = "PW";

	private Fragment fragment;
	private FragmentManager frgManager;
	private ArrayList<String> item;
	private EditText tvId;
	private EditText tvPw;
	private EditText tvUrl;
	private EditText tvName;
	private Spinner spin;
	private InfoDao info;
	private ArrayAdapter<CharSequence> adspin;
	private Button btn;
	private MyDatabaseManager mIdb;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "ItemUpdate onCreateView");
		View view = inflater.inflate(R.layout.item_update, container, false);
		item = getArguments().getStringArrayList("item");
		info = (InfoDao) getActivity().getApplication();
		info.setMain(false);
		tvId = (EditText) view.findViewById(R.id.id);
		tvPw = (EditText) view.findViewById(R.id.password);
		tvUrl = (EditText) view.findViewById(R.id.url);
		tvName = (EditText) view.findViewById(R.id.name);
		
		spin = (Spinner) view.findViewById(R.id.alarmSpinner);
		spin.setPrompt("주기를 선택하세요.");
		adspin = ArrayAdapter.createFromResource(getActivity(), R.array.alarm,
				android.R.layout.simple_spinner_item);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(adspin);
		
		btn = (Button) view.findViewById(R.id.btn);
		btn.setOnClickListener(this);
		tvId.setText(item.get(0));
		tvPw.setText(item.get(1));
		tvUrl.setText(item.get(2));
		tvName.setText(item.get(3));
		spin.setSelection(Integer.parseInt(item.get(4)));
		
		return view;
	}

	@Override
	public void onClick(View v) {
		Log.d(TAG, "ItemUpdate onClick");
		switch (v.getId()) {
		case R.id.btn:
			Toast.makeText(getActivity(), spin.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
			String exName = item.get(3);
			item.set(0, tvId.getText().toString());
			item.set(1, tvPw.getText().toString());
			item.set(2, tvUrl.getText().toString());
			item.set(3, tvName.getText().toString());
			item.set(4, spin.getSelectedItemPosition()+"");
			item.set(5, info.getDate(Calendar.getInstance().getTime()));
			mIdb = MyDatabaseManager.getInstance(getActivity());
			ContentValues addRowValue = new ContentValues();
			addRowValue.put("id", item.get(0));
			addRowValue.put("password", item.get(1));
			addRowValue.put("url", item.get(2));
			addRowValue.put("name", item.get(3));
			addRowValue.put("cycle", item.get(4));
			addRowValue.put("date", item.get(5));
			mIdb.update("list", addRowValue, "name=? and account=?", new String[] { exName, item.get(6) });
			Bundle args = new Bundle();
			args.putString(ItemView.ITEM_NAME, item.get(3));
			fragment = new ItemView();
			fragment.setArguments(args);
			frgManager = getFragmentManager();
			frgManager.beginTransaction().replace(R.id.content_frame, fragment)
					.commit();
			break;
		case R.id.btnRC:
			break;
		}
	}
}
